﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.BusinessLayer;
using System.Collections;

namespace BSCMFF.Web.Pages
{
    public partial class UserListMoore : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            //BSCEntities context = new BSCEntities();

            User u = new User();
            u.Login = (userControl.FindControl("TextBoxLogin") as RadTextBox).Text;
            u.Password = (userControl.FindControl("TextBoxPassword") as RadTextBox).Text;
            u.Role_FK = Convert.ToInt32((userControl.FindControl("ComboBoxRoles") as RadComboBox).SelectedValue);
            u.User_Setting_FK = 1;
            //u.Role = new Role();

            //u.Login = "agtwe";
            //u.Password = "adsg";
            //u.Role_FK = 1;
            //u.User_Setting_FK = 1;
            UserManager um = new UserManager();
            um.CreateUser(u);

            //context.Users.AddObject(u);


            //context.SaveChanges();

        }
        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);


            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"].ToString());

            UserManager um = new UserManager();
            um.Delete(id);

            //BSCEntities context = new BSCEntities();

            //User u = (from user in context.Users
            //          where user.User_ID == id
            //          select user).Single();

            //context.Users.DeleteObject(u);
            //context.SaveChanges();
            RadGrid1.Rebind();
        }

        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);


            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"].ToString());
            BSCEntities context = new BSCEntities();

            //User u = (from user in context.Users
            //          where user.User_ID == id
            //          select user).Single();

            Hashtable ht = new Hashtable();
            ht["login"] = (userControl.FindControl("TextBoxLogin") as RadTextBox).Text;
            //u.Login = (userControl.FindControl("TextBoxLogin") as RadTextBox).Text;
            ht["password"] = (userControl.FindControl("TextBoxPassword") as RadTextBox).Text;            
            //u.Password = (userControl.FindControl("TextBoxPassword") as RadTextBox).Text;
            ht["role_fk"] = Convert.ToInt32((userControl.FindControl("ComboBoxRoles") as RadComboBox).SelectedValue);
            //u.Role_FK = Convert.ToInt32((userControl.FindControl("ComboBoxRoles") as RadComboBox).SelectedValue);
            //context.SaveChanges();

            UserManager um = new UserManager();
            //um.Update(id, ht);
        }

        //protected void RadGrid1_SelectedIndexChanged(object source, GridCommandEventArgs e)
        //{
        //    Response.Redirect("UserList.aspx?EmployeeID=" + RadGrid1.SelectedValue);
        //}

        //protected void RadGrid1_SelectedIndexChanged1(object sender, EventArgs e)
        //{
            
        //}

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item.ItemIndex != -1) //create new
            {
                int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"].ToString());
                Response.Redirect("EmployeeInfo.aspx?EmployeeID=" + id);
            }
        }
    }
}